/**
 * The Screen class handles displaying of Objects to the Grid
 * 
 * @author Adrian
 * @version 0.01
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Screen
{
    private JTextArea text;
    private JTextArea status;
    private Font font;
    private JPanel panel;
    private int scrollInterval = 0;

    /**
     * Constructor for objects of class Screen
     */
    public Screen(JTextArea newText, JTextArea stati)
    {
        //initialize instance var
        text = newText;
        status = stati;
        font = new Font("Consolas", Font.BOLD, 14);

        //set text variables
        text.setLineWrap(false);
        text.setEditable(false);
        text.setFont(font);
        text.setForeground(Color.LIGHT_GRAY); //
        text.setBackground(Color.BLACK); //

        status.setLineWrap(false);
        status.setEditable(false);
        status.setFont(font);
        status.setForeground(Color.WHITE); 
        status.setBackground(Color.BLACK);
        
        //fill with void characters
        clearScreen();

    }

    /**
     * The display() method replaces each entity at coord
     *  
     * @pre     
     * @param   noone
     * @return  none     
     * @post    new character at coord
     */
    public void display(){
        clearScreen();
        //Go through each entity and display it one by one.
        for (GridEntity ent: Grid.getEntities())
        {
            replaceAt(ent.getCoordX(), ent.getCoordY(), ent.getSymbol());       
        }
    }

    /**
     * The replaceAt() method changes a character at a certain point
     *  
     * @pre     
     * @param   column,row,c
     * @return  none     
     * @post    new char at point
     */
    public void replaceAt(int column, int row, char c){
        if (row >= 0 && row < Grid.height && column >= 0 && column < Grid.width)
        {
            int start = ((row * Grid.width) + column) * 2;
            text.replaceRange(c+"", start, start + 1);
            //Fill gaps between horizontal walls
            if (c == '█' && column != Grid.width - 1)
            {
                GridEntity ent = Grid.getEntAt(column + 1, row);
                if (ent != null && ent.getSymbol() == c)
                    text.replaceRange(c+"", start+1, start+2);
            }
        }
    }

    /**
     * The clearScreen() method clears the screen
     *  
     * @pre     
     * @param   none
     * @return  none     
     * @post    new blank screen
     */
    private void clearScreen()
    {
        //Used for the scrolling CRT monitor effect.
        scrollInterval = (scrollInterval + 1) % Grid.height;
        int scrollInt2 = (scrollInterval + Grid.height/2) % Grid.height;
        
        //Empty the screen.
        text.setText("");
        
        //Fill screen with a grid of dots.
        for (int i = 0; i < Grid.height; i++)
        {
            int distA = Math.abs(i - scrollInterval);
            int distB = Grid.height - distA;
            int distC = Math.abs(i - scrollInt2);
            int distD = Grid.height - distC;
            for (int j = 0; j < Grid.width; j++)
            {
                //Cut out dots in line with player to make it easier to find player.
                if (i == Runner.player.getCoordY() || j == Runner.player.getCoordX())
                    text.append(" ");
                //If the dot is within range of one of the scrolling intervals, it will make a static effect.
                else if (Math.random() > 0.8 && (distA <= 2 | distB <= 2 | distC <= 2 | distD <= 2))
                    text.append("=");
                else
                    text.append("-");
                //Space out the dots horizontally.
                if (j < Grid.width - 1)
                    text.append(" ");
            }
            if (i < Grid.height - 1)
                text.append("\n");
        }    
    }
}